package com.bw.shop2208a.login

import android.app.AlertDialog
import android.content.DialogInterface
import android.content.Intent
import android.view.LayoutInflater
import android.widget.EditText
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.shop2208a.MainActivity
import com.bw.shop2208a.R
import com.bw.shop2208a.databinding.ActivityLoginBinding
import com.bw.shop2208a.db.DBUtils
import com.bw.shop2208a.entity.LoginParam
import com.bw.shop2208a.mvvm.BaseActivity
import com.bw.shop2208a.utils.Constant
import kotlinx.coroutines.launch
import java.util.zip.Inflater

/**
 * @Route路由注解定义跳转界面的路径名称  名称规范/XXX/XXX 同一个App下前方名称一致 不同App下前方名称不能相同
 */
@Route(path = "/app/login")
class LoginActivity : BaseActivity<ActivityLoginBinding, UserViewModel>() {
    override fun initViwe() {
        binding.btn1.setOnClickListener {
            val user = binding.user.text.toString()
            val pass = binding.pass.text.toString()
            if (user.isNotEmpty()||pass.isNotEmpty()){
                if (!RegexUtils.isMobileSimple(user)){
                    ToastUtils.showLong("用户名不符合规范")
                    return@setOnClickListener
                }
                val loginParam = LoginParam(pass, user)
                viewModel.login(loginParam)
            }else{
                ToastUtils.showLong("用户名密码不能为空")
            }
        }
        binding.tv.setOnClickListener {
//            val intent = Intent(this, RegisterActivity::class.java)
//            startActivityForResult(intent,101)
            ARouter.getInstance().build("/app/register").navigation(this,101)
        }
    }

    override fun iniData() {
        viewModel.success_login.observe(this){
            ToastUtils.showLong(it.message)
            if (it.code==200){
                SPUtils.getInstance().put(Constant.SP_TOKEN,it.data.token)
                SPUtils.getInstance().put("id",it.data.id)
                ARouter.getInstance().build("/app/main").withSerializable("user",it.data).navigation()
                lifecycleScope.launch {
                    DBUtils.getMyDataBaer().getUserDao().insertUser(it.data)
                }
            }
        }
        viewModel.fail_login.observe(this){
            ToastUtils.showLong(it)
        }
    }

    //接收注册成功后的返回值
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode==101&&resultCode==102){
            val user = data?.extras?.getString("user")
            val pass = data?.extras?.getString("pass")
            binding.user.setText(user)
            binding.pass.setText(pass)
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_login
    override fun getViewModel(): Class<UserViewModel> = UserViewModel::class.java
}